﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using PloobsEngine.Modelo;

namespace PloobsEngine.Material
{
    public class CilindricGPUBilboardForward : IShader
    {
        public CilindricGPUBilboardForward()
        {
            Initialize();
        }

        public CilindricGPUBilboardForward(Vector2 scale)
        {
            this.scale = scale;
            Initialize();
        }
        
        private Effect _shader;
        private Vector2 scale = Vector2.One;

        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        private Vector3 allowedRotDir = new Vector3(0, 1, 0);

        /// <summary>
        /// Default Vector3(0, 1, 0)
        /// </summary>
        public Vector3 AllowedRotDir
        {
            get { return allowedRotDir; }
            set { allowedRotDir = value; }
        }

        public override MaterialType MaterialType
        {
            get { return MaterialType.FORWARD; }
        }

        public override void  PosDrawPhase(IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam, IList<PloobsEngine.Light.ILight> lights)
        {
 	        _shader.Parameters["scale"].SetValue(scale);
            _shader.Parameters["xWorld"].SetValue(Matrix.Identity);
            _shader.Parameters["xView"].SetValue(cam.View);
            _shader.Parameters["xProjection"].SetValue(cam.Projection);
            _shader.Parameters["xCamPos"].SetValue(cam.Position);
            _shader.Parameters["xAllowedRotDir"].SetValue(allowedRotDir);
            _shader.Parameters["xBillboardTexture"].SetValue(modelo.getTexture(TextureType.DIFFUSE));

            _shader.Begin();
            BatchInformation batchInfo = modelo.GetBatchInformation(0)[0];
            EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(0), batchInfo.StreamOffset, batchInfo.VertexStride);
            EngineStuff.GraphicsDevice.VertexDeclaration = batchInfo.VertexDeclaration;
            {
                    ///ORDEM DE TRANSPARENCIA CORRETA SEM SORT --maloqueragem pura        
                    EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = true;
                    EngineStuff.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.GreaterEqual;
                    EngineStuff.GraphicsDevice.RenderState.ReferenceAlpha = 200;

                    _shader.CurrentTechnique.Passes[0].Begin();
                    EngineStuff.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, batchInfo.StartVertex, batchInfo.PrimitiveCount);
                    _shader.CurrentTechnique.Passes[0].End();
                    
                    EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
                    EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = true;
                    EngineStuff.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
                    EngineStuff.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;

                    EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = true;
                    EngineStuff.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Less;
                    EngineStuff.GraphicsDevice.RenderState.ReferenceAlpha = 200;

                    _shader.CurrentTechnique.Passes[0].Begin();
                    EngineStuff.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, batchInfo.StartVertex, batchInfo.PrimitiveCount);
                    _shader.CurrentTechnique.Passes[0].End();
            

                EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = false;
                EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
                EngineStuff.GraphicsDevice.RenderState.AlphaTestEnable = false;  

            }               
            
            
            _shader.End();

            EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = false;
        }


        public override void  Initialize()
        {
            this._shader = EngineStuff.InternalContentManager.GetAsset<Effect>("cilBilboardForward");
            _shader.CurrentTechnique = _shader.Techniques["CylBillboard"];
        }


    


    }
}
